<%-- 
  -- Body New para Course.
  --
  -- @author ariel.piechotka
--%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html-el-1.2.8.tld" prefix="html-el"%>
<%@ taglib uri="/WEB-INF/struts-logic-el-1.2.8.tld" prefix="logic-el"%>
<%@ taglib uri="/WEB-INF/security.tld" prefix="security" %>

<%@ taglib uri="http://struts.apache.org/tags-bean-el" prefix="bean2"%>

<div class="errors">
    <html:errors />
</div>

<link rel="stylesheet" type="text/css" href="<html:rewrite page='/protected/style/dragdrop.css'/>" id="styleFile"/>

<%-- prototype esta en el main layout --%>
<script src="<html:rewrite page='/protected/script/util.js'/>" type="text/javascript"></script>

<%-- Para EXT --%>
<link rel="stylesheet" type="text/css" href="<html:rewrite page='/protected/script/ext-1.1.1/resources/css/ext-all.css'/>" />

<script src="<html:rewrite page='/protected/script/ext-1.1.1/adapter/ext/ext-base.js'/>" type="text/javascript"></script>
<script src="<html:rewrite page='/protected/script/ext-1.1.1/ext-all.js'/>" type="text/javascript"></script>

<script src="<html:rewrite page='/protected/script/calendar/calendar.js'/>" type="text/javascript"></script>
<script src="<html:rewrite page='/protected/script/calendar/calendar-setup.js'/>" type="text/javascript"></script>
<script src="<html:rewrite page='/protected/script/calendar/lang/calendar-es.js'/>" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<html:rewrite page='/protected/script/calendar/style/calendar-blue.css'/>" id="styleFileAnyId"/>



<div class="errors">
    <html:errors />
</div>


<script type="text/javascript">
    /** Guarda las cuotas */
    function modifyInstalments() {

        var registrationFeeValueValue = $("registrationFeeValueId").value;
        var registrationFeeQtyValue = $("registrationFeeQtyId").value;
        var debtTypeSelectedRegFeeValue = $("debtTypeSelectedRegFeeId").value;
        var debtTypeSelectedInstValue = $("debtTypeSelectedInstId").value;

        if(!Util.valid.isBlank(registrationFeeValueValue) && isNaN(registrationFeeValueValue)) {
        	Ext.MessageBox.alert('Error', 'Debe ingresar un valor numerico para la matricula. ');
        	$("registrationFeeValueId").focus();
        	return;
        }
        if (!Util.valid.isBlank(registrationFeeValueValue) && Util.valid.isBlank(debtTypeSelectedRegFeeValue)) {
            Ext.MessageBox.alert('Error', 'Debe ingresar el tipo de concepto para la matricula. ');
            $("debtTypeSelectedRegFeeId").focus();
            return;
        }
        if (Util.valid.isBlank(debtTypeSelectedInstValue)) {
            Ext.MessageBox.alert('Error', 'Debe ingresar el tipo de concepto para las cuotas. ');
            $("debtTypeSelectedInstId").focus();
            return;
        }
        

        var instalments = "";
        var i = 0;
        while (true) {
        	var elem = document.getElementsByName('instalmentsValues['+i+']');
			if (elem.length == 0)
				break;
			else
				elem = elem[0];
				
			if(Util.valid.isBlank(elem.value)) {
            	Ext.MessageBox.alert('Error', 'Debe ingresar un valor para todas las cuotas (puede ser cero). ' +
            		'Si desea ingresar menos cuotas que las ofrecidas, seleccione un numero de cuotas menor. ');
            	elem.focus();
            	return;
			}
            else if (isNaN(elem.value)) {
            	Ext.MessageBox.alert('Error', 'Debe ingresar un valor numerico para la cuota ' + (i + 1) + '. ');
            	elem.focus();
            	return;
            }
            else
            	instalments += elem.value + ";";
            i++;
        }
        if(instalments.length > 0)
        	instalments = instalments.substring(0, instalments.length - 1);

        var courseObj = $("courseModifyId");
        var registrationFeeValueObj = $("registrationFeeValueModifyId");
        var registrationFeeQtyObj = $("registrationFeeQtyModifyId");
        var instalmentsObj = $("instalmentsId");
        var debtTypeSelectedRegFeeObj = $("debtTypeSelectedRegFeeModifyId");
        var debtTypeSelectedInstObj = $("debtTypeSelectedInstModifyId");
        
		courseObj.value = $("courseId").value;
        registrationFeeValueObj.value = registrationFeeValueValue;
        registrationFeeQtyObj.value = registrationFeeQtyValue;
        instalmentsObj.value = instalments;
        debtTypeSelectedRegFeeObj.value = debtTypeSelectedRegFeeValue;
        debtTypeSelectedInstObj.value = debtTypeSelectedInstValue;
        
        var form = $("modifyInstalmentsFormId");
        form.submit();
    } 
    
    function copyFirstToAll() {
        var firstValue = document.getElementsByName('instalmentsValues[0]')[0].value;
        var i = 0;
        while (++i) {
        	var elem = document.getElementsByName('instalmentsValues['+i+']');
			if (elem.length == 0)
				break;
			else
				elem[0].value = firstValue;
        }
    }
</script>

<%-- El ID del form NO debe ser modificado. Se usa desde el menu --%>
<html:form action="/protected/showInstalments" styleId="courseFormId">
    <%-- Inyectados por Javascript --%>
    <html:hidden name="instalmentForm" property="courseId" styleId="courseId"/>

    <h2>Cuotas del curso</h2>
    
    <div class="instalmentsEdit">
        <table class="tableData" cellspacing="0">
            <tr>
                <td class="tableDataTitle">
                    Matr&iacute;cula
                </td>
                <td class="tableDataValue">
                    <html:text property="registrationFeeValue" styleId="registrationFeeValueId" alt="Valor de la matricula" maxlength="9"/>
                </td>
            </tr>
            <tr>
                <td class="tableDataTitle">
                    Cantidad de matr&iacute;culas
                </td>
                <td class="tableDataValue">
                    <html:select property="qtyRegFees" styleClass="allSelects" styleId="registrationFeeQtyId">
                        <html:optionsCollection property="qtyRegFeesOptions"/>
                    </html:select>
                </td>
            </tr>
            <tr>
                <td class="tableDataTitle">
                    Matr&iacute;cula: tipo de concepto
                </td>
                <td class="tableDataValue">
                    <html:select property="debtTypeSelectedRegFee" styleClass="allSelects" styleId="debtTypeSelectedRegFeeId" >
                        <html:optionsCollection property="debtTypeOptions"/>
                    </html:select>
                </td>            
            </tr>

            <tr>
                <td class="tableDataTitle">
                    Cuotas
                </td>
                <td class="tableDataValue">
                    <html:select property="qtyInstalments" styleClass="allSelects" styleId="qtyInstalmentsId" onchange="javascript:doReload();">
                        <html:optionsCollection property="qtyInstalmentsOptions"/>
                    </html:select>
                    <logic:greaterThan name="instalmentForm" property="qtyInstalments" value="1">
	                    <input type="button" onclick="copyFirstToAll();" value="Copiar primera cuota a todas"/>
                    </logic:greaterThan>
                </td>            
            </tr>
            <logic-el:iterate id="instalmentName" name="instalmentForm" property="instalmentsNames" indexId="idx">
	            <tr>
	                <td class="tableDataTitle">
	                    <bean:write name="instalmentName" />
	                </td>
	                <td class="tableDataValue">
	                    <html:text property="instalmentsValues[${idx}]" styleId="instalmentValueId" alt="Valor de la cuota" maxlength="9"/>
	                </td>            
	            </tr>
			</logic-el:iterate>
            <tr>
                <td class="tableDataTitle">
                    Cuotas: tipo de concepto
                </td>
                <td class="tableDataValue">
                    <html:select property="debtTypeSelectedInst" styleClass="allSelects" styleId="debtTypeSelectedInstId" >
                        <html:optionsCollection property="debtTypeOptions"/>
                    </html:select>
                </td>            
            </tr>
        </table>
        
        <%-- No se valida seguridad ya que SOLO entran aca los q tienen permiso --%>
        <div class="panelButton">
            <input type="button" value="Guardar" onclick="modifyInstalments();" title="Guarda las cuotas..." class="button">
        </div>  
    
    </div>

</html:form>

<%-- Inyectados por Javascript --%>
<div id="modifyDivId" class="hideClass">
    <html:form action="/protected/modifyInstalments" styleId="modifyInstalmentsFormId">
        <html:hidden property="courseId" value="" styleId="courseModifyId" />
        <html:hidden property="registrationFeeValue" value="" styleId="registrationFeeValueModifyId" />
        <html:hidden property="registrationFeeQty" value="" styleId="registrationFeeQtyModifyId" />
        <html:hidden property="instalments" value="" styleId="instalmentsId" />
        <html:hidden property="debtTypeSelectedRegFee" value="" styleId="debtTypeSelectedRegFeeModifyId" />
        <html:hidden property="debtTypeSelectedInst" value="" styleId="debtTypeSelectedInstModifyId" />
    </html:form>
</div>
